package org.turman.buobaobook.config;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.turman.buobaobook.quartz.BookDownloadJob;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class ScheduleConfig {

    @Bean
    public JobDetail bookDownloadJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(BookDownloadJob.class)
                .withIdentity("BookDownloadJob")
                .storeDurably()
//                .usingJobData("data", "保密信息")
                .build();
        return jobDetail;
    }

    /**
     * 下载执行器
     * 周一至周五的上午10:15触发
     * 0 15 10 ? * 2,3,4,5,6
     * 表示在每月的1日的凌晨2点调整任务
     * 0 0 2 1 * ?
     * 朝九晚五工作时间内每半小时
     * 0 00,30 9-17 * * ?
     * 每天上午10点，下午2点，4点
     * 0 0 10,2,4 * * ?
     * 表示每个星期三中午12点
     * 0 0 12 ? * 4
     * 在每天下午2点到下午2:59期间的每1分钟触发
     * 0 0-59/1 14 * * ?
     * 在每天下午2点到下午2:05期间的每1分钟触发
     * 0 0-5 14 * * ?
     * 在每天下午2点到下午2:55期间的每5分钟触发
     * 0 0-55/5 14 * * ?
     * 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
     * 0 0-55/5 14,18 * * ?
     */
    @Bean
    public Trigger bookDownloadTrigger() {
        String cronExpression = "0 59 17 ? * 2,3,4,5,6";
        return TriggerBuilder.newTrigger()
                .forJob(bookDownloadJobDetail())
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                .build();

//        Trigger trigger = TriggerBuilder.newTrigger()
//                .forJob(bookDownloadJobDetail())
//                .withSchedule(simpleSchedule()
//                        .withIntervalInSeconds(3)
//                        .repeatForever())
//                .build();
//        return trigger;
    }

    @Bean
    @DependsOn("dataSource")
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);

        Properties properties = new Properties();
        properties.put("org.quartz.scheduler.instanceName", "BookDownloadScheduler");
        properties.put("org.quartz.scheduler.instanceId", "AUTO");
        //线程池配置
        properties.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        properties.put("org.quartz.threadPool.threadCount", "20");
        properties.put("org.quartz.threadPool.threadPriority", "5");
        //JobStore配置
        properties.put("org.quartz.jobStore.class", "org.springframework.scheduling.quartz.LocalDataSourceJobStore");
        //集群配置
        properties.put("org.quartz.jobStore.isClustered", "true");
        properties.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        properties.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        properties.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");

        properties.put("org.quartz.jobStore.misfireThreshold", "12000");
        properties.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
        schedulerFactoryBean.setQuartzProperties(properties);

        // 延时启动
        schedulerFactoryBean.setStartupDelay(1);
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
        // 可选，QuartzScheduler
        // 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        // 设置自动启动，默认为true
        schedulerFactoryBean.setAutoStartup(true);

        return schedulerFactoryBean;
    }

//    @Bean
//    public Scheduler scheduler(SchedulerFactoryBean factory) throws IOException {
//        return factory.getScheduler();
//    }
}
